package com.tencent.wxcloudrun.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.wxcloudrun.common.Result;
import com.tencent.wxcloudrun.common.annotation.NotNUll;
import com.tencent.wxcloudrun.common.component.WeChatCommon;
import com.tencent.wxcloudrun.project.entity.NewOrder;
import com.tencent.wxcloudrun.project.entity.Norms;
import com.tencent.wxcloudrun.project.entity.Order;
import com.tencent.wxcloudrun.project.entity.muti.OrderMuti;
import com.tencent.wxcloudrun.project.entity.query.OrderQuery;
import com.tencent.wxcloudrun.project.mapper.NewOrderMapper;
import com.tencent.wxcloudrun.project.mapper.OrderMapper;
import com.tencent.wxcloudrun.project.service.NewOrderService;
import com.tencent.wxcloudrun.project.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private NewOrderMapper newOrderMapper;
    @Autowired
    public WeChatCommon weChatCommon;

    @GetMapping("/pageList")
    public Result pageList(@RequestParam(defaultValue = "") String customId,
                           @RequestParam(defaultValue = "") Integer status,
                           @RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "8") int pageSize
    ){
        OrderQuery orderQuery=new OrderQuery();
        orderQuery.setCustomId(customId);
        orderQuery.setStatus(status);
        PageHelper.startPage(pageNum, pageSize);
        NewOrder newOrder = new NewOrder();
        Integer Num=0;
        newOrder.setNum(Num);
        QueryWrapper<NewOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",1);
        newOrderMapper.update(newOrder,queryWrapper);
        PageInfo<OrderMuti> pageInfo = new PageInfo<>(orderService.pageList(orderQuery));
        return Result.success(pageInfo);
    }
    @GetMapping("/orderList")
    public Result orderList(@RequestParam(defaultValue = "") Long id,
                           @RequestParam(defaultValue = "") Integer status,
                           @RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "8") int pageSize
    ){
        OrderQuery orderQuery=new OrderQuery();
        orderQuery.setId(id);
        orderQuery.setStatus(status);
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<OrderMuti> pageInfo = new PageInfo<>(orderService.pageList(orderQuery));
        return Result.success(pageInfo);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Order order){
        orderMapper.insert(order);
        return Result.success(order.getId());
    }
    @GetMapping("/select")
    public Result select(@NotNUll @RequestParam Long orderID){
        return Result.success(orderService.getById(orderID));
    }
    @PostMapping("/update")
    public Result update(@RequestBody Order order){
        if(order.getStatus().getValue()==1){
            weChatCommon.sendSubscribe1(order);
        }
        if(order.getStatus().getValue()==2){
            weChatCommon.sendSubscribe2(order);
        }
        return Result.success(orderService.saveOrUpdate(order));
    }
    @GetMapping("/menuList")
    public Result menuList(@NotNUll @RequestParam Long orderID
    ){
        return Result.success(orderService.menuList(orderID));
    }
}
